import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import CategoryCreate from '../views/category/CategoryCreate'
import CategoryList from '../views/category/CategoryList'
import EquipList from '../views/equip/EquipList'
import EquipCreate from '../views/equip/EquipCreate'
import HeroList from '../views/hero/HeroList'
import HeroCreate from '../views/hero/HeroCreate'
import ArticleList from '../views/article/ArticleList'
import ArticleCreate from '../views/article/ArticleCreate'
import AdList from '../views/ad/AdList'
import AdCreate from '../views/ad/AdCreate'
import Welcome from '../views/Welcome.vue'
import AdminList from '../views/admin/AdminList'
import Login from '../views/Login'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login, meta: { isPublic: true } },
  {
    path: '/',
    name: 'Home',
    redirect: '/welcome',
    component: Home,
    children: [
      { path: 'welcome', component: Welcome },
      { path: 'categories/create', component: CategoryCreate },
      { path: 'categories/list', component: CategoryList },
      { path: 'equips/create', component: EquipCreate },
      { path: 'equips/list', component: EquipList },
      { path: 'heroes/create', component: HeroCreate },
      { path: 'heroes/list', component: HeroList },
      { path: 'heroes/edit/:id', component: HeroCreate, props: true },
      { path: 'articles/create', component: ArticleCreate },
      { path: 'articles/list', component: ArticleList },
      { path: 'articles/edit/:id', component: ArticleCreate, props: true },
      { path: 'ads/create', component: AdCreate },
      { path: 'ads/list', component: AdList },
      { path: 'admin_users/list', component: AdminList },

    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//在每一次切换路由的时候，要做什么
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !sessionStorage.getItem('token')) {
    return next('/login')
  }
  next()
})
export default router
